package com.bsoft.gol.hcs.clinical.dao;

import com.bsoft.gol.hcs.clinical.bean.checkstack.request.CommonQo;
import com.bsoft.gol.hcs.clinical.entity.OdsMedicalTechnologySet;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import com.bsoft.gol.hcs.utils.CommonUtils;

import java.util.List;

/**
 * @Author impel
 * @Date 2021/5/7 13:29
 * @Description:
 * @Version 1.0
 */
public abstract class OdsMedicalTechnologySetDAO extends HibernateSupportDelegateDAO<OdsMedicalTechnologySet> {

    public OdsMedicalTechnologySetDAO(){
        this.setKeyField("medicalTechnologySetId");
    }

    public List<OdsMedicalTechnologySet> queryOdsMedicalTechnologySet(final CommonQo qo){
        HibernateStatelessResultAction<List<OdsMedicalTechnologySet>> action = new AbstractHibernateStatelessResultAction<List<OdsMedicalTechnologySet>>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append(" from OdsMedicalTechnologySet where 1=1 ");
                if (CommonUtils.isNotEmpty(qo.getOrganizationId())) {
                    stringBuffer.append(" and organizationId =:organizationId ");
                }
                if (CommonUtils.isNotEmpty(qo.getDocId())) {
                    stringBuffer.append(" and docId =:docId ");
                }
                if (CommonUtils.isNotEmpty(qo.getDepartmentId())) {
                    stringBuffer.append(" and departmentId =:departmentId ");
                }
                if (CommonUtils.isNotEmpty(qo.getAttributionType())) {
                    stringBuffer.append(" and attributionType =:attributionType ");
                }
                if (CommonUtils.isNotEmpty(qo.getSetType())) {
                    stringBuffer.append(" and setType =:setType  ");
                }
                if (0 != qo.getMedicalTechnologySetId()) {
                    stringBuffer.append(" and medicalTechnologySetId =:medicalTechnologySetId ");
                }

                stringBuffer.append( " order by sort ");
                Query sqlQuery = ss.createQuery(stringBuffer.toString());
                if (CommonUtils.isNotEmpty(qo.getOrganizationId())) {
                    sqlQuery.setParameter("organizationId",qo.getOrganizationId());
                }
                if (CommonUtils.isNotEmpty(qo.getDocId())) {
                    sqlQuery.setParameter("docId",qo.getDocId());
                }
                if (CommonUtils.isNotEmpty(qo.getDepartmentId())) {
                    sqlQuery.setParameter("departmentId",qo.getDepartmentId());
                }
                if (CommonUtils.isNotEmpty(qo.getAttributionType())) {
                    sqlQuery.setParameter("attributionType",qo.getAttributionType());
                }
                if (CommonUtils.isNotEmpty(qo.getSetType())) {
                    sqlQuery.setParameter("setType",qo.getSetType());
                }
                if (0 != qo.getMedicalTechnologySetId()) {
                    sqlQuery.setParameter("medicalTechnologySetId",qo.getMedicalTechnologySetId());
                }
                setResult(sqlQuery.list());
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }


    public List<OdsMedicalTechnologySet> findOdsMedicalTechnologySetList(final OdsMedicalTechnologySet qo){
        HibernateStatelessResultAction<List<OdsMedicalTechnologySet>> action = new AbstractHibernateStatelessResultAction<List<OdsMedicalTechnologySet>>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append("from OdsMedicalTechnologySet where 1=1 ");
                if (CommonUtils.isNotEmpty(qo.getOrganizationId())) {
                    stringBuffer.append(" and organizationId = '" + qo.getOrganizationId()+"'");
                }
                if (CommonUtils.isNotEmpty(qo.getDepartmentId())) {
                    stringBuffer.append(" and departmentId = '" + qo.getDepartmentId()+"'");
                }
                if (CommonUtils.isNotEmpty(qo.getAttributionType())) {
                    stringBuffer.append(" and attributionType = '" + qo.getAttributionType()+"'");
                }
                if (CommonUtils.isNotEmpty(qo.getSetType())) {
                    stringBuffer.append(" and setType = '" + qo.getSetType()+"'");
                }
                if(CommonUtils.isNotEmpty(qo.getDocId())){
                    stringBuffer.append(" and docId = '" + qo.getDocId()+"'");
                }
                stringBuffer.append(" order by sort ");
                Query sqlQuery = ss.createQuery(stringBuffer.toString());
                setResult(sqlQuery.list());
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    public List<OdsMedicalTechnologySet> findBeforeOdsMedicalTechnologySetList(final OdsMedicalTechnologySet qo){
        HibernateStatelessResultAction<List<OdsMedicalTechnologySet>> action = new AbstractHibernateStatelessResultAction<List<OdsMedicalTechnologySet>>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append("from OdsMedicalTechnologySet where 1=1 ");
                if (CommonUtils.isNotEmpty(qo.getOrganizationId())) {
                    stringBuffer.append(" and organizationId = '" + qo.getOrganizationId()+"'");
                }
                if (CommonUtils.isNotEmpty(qo.getDepartmentId())) {
                    stringBuffer.append(" and departmentId = '" + qo.getDepartmentId()+"'");
                }
                if (CommonUtils.isNotEmpty(qo.getAttributionType())) {
                    stringBuffer.append(" and attributionType = '" + qo.getAttributionType()+"'");
                }
                if (CommonUtils.isNotEmpty(qo.getSetType())) {
                    stringBuffer.append(" and setType = '" + qo.getSetType()+"'");
                }
                if(CommonUtils.isNotEmpty(qo.getDocId())){
                    stringBuffer.append(" and docId = '" + qo.getDocId()+"'");
                }
                if(0 != qo.getSort()){
                    stringBuffer.append(" and sort > " + qo.getSort()+"");
                }
                stringBuffer.append(" order by sort asc");

                Query sqlQuery = ss.createQuery(stringBuffer.toString());
                setResult(sqlQuery.list());
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    public List<OdsMedicalTechnologySet> findAfterOdsMedicalTechnologySetList(final OdsMedicalTechnologySet qo){
        HibernateStatelessResultAction<List<OdsMedicalTechnologySet>> action = new AbstractHibernateStatelessResultAction<List<OdsMedicalTechnologySet>>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append("from OdsMedicalTechnologySet where 1=1 ");
                if (CommonUtils.isNotEmpty(qo.getOrganizationId())) {
                    stringBuffer.append(" and organizationId = '" + qo.getOrganizationId()+"'");
                }
                if (CommonUtils.isNotEmpty(qo.getDepartmentId())) {
                    stringBuffer.append(" and departmentId = '" + qo.getDepartmentId()+"'");
                }
                if (CommonUtils.isNotEmpty(qo.getAttributionType())) {
                    stringBuffer.append(" and attributionType = '" + qo.getAttributionType()+"'");
                }
                if (CommonUtils.isNotEmpty(qo.getSetType())) {
                    stringBuffer.append(" and setType = '" + qo.getSetType()+"'");
                }
                if(CommonUtils.isNotEmpty(qo.getDocId())){
                    stringBuffer.append(" and docId = '" + qo.getDocId()+"'");
                }
                if(0 != qo.getSort()){
                    stringBuffer.append(" and sort < " + qo.getSort()+"");
                }
                stringBuffer.append(" order by sort desc");

                Query sqlQuery = ss.createQuery(stringBuffer.toString());
                setResult(sqlQuery.list());
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }
}
